create or replace function selectPosterminalModel(
	out refcursor, inout xml
	)
as
$$
	declare
		ref alias for $1;
		xmlQueryData alias for $2;
		vProducerId integer := null;
		vIdProducerNode varchar(100) := '/posterminalproducer/@id';
	begin
		-- get data from xml
		vProducerId := cast(extract_value(vIdProducerNode, xmlQueryData) as int);
	
		-- if no data for select return all data
		if (vProducerId is null) then begin
			-- return data
			open ref for
				select
					posterminalModel2Xml(
						poel.idPosterminalModel,
						poel.namePosterminalModel,
						posterminalProducer2xml(poer.idPosterminalProducer, poer.namePosterminalProducer)
					)
					from posterminalProducer poer, posterminalModel poel
					where
						poer.idPosterminalProducer = poel.idPosterminalProducer
			;
		end;
		else begin
	
		
		-- return data
		open ref for
			select
				posterminalModel2Xml(
					poel.idPosterminalModel,
					poel.namePosterminalModel,
					posterminalProducer2xml(poer.idPosterminalProducer, poer.namePosterminalProducer)
				)
				from posterminalProducer poer, posterminalModel poel
				where
					poer.idPosterminalProducer = poel.idPosterminalProducer
					and poer.idPosterminalProducer = vProducerId
			;
		end;
		end if;
		
		return;
	end;
$$ language plpgsql	stable;